//
//  ClassifyManager.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/28.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "ClassifyManager.h"

static ClassifyManager *_instance;

@implementation ClassifyManager

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    }) ;
    return _instance ;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _dataSource = [NSMutableArray array];
    }
    return self;
}

- (void)setupAllClassify {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    __weak typeof(self) weakSelf = self;
    [MYAFNetworking POST:URL_Get_All_Class parameters:parameters success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        NSDictionary *responseDic = responseObject;
        NSString *code = [responseDic safeStringForKey:@"code"];
        if (code && [code isEqualToString:@"200"]) {
            NSArray *data = [responseDic safeJsonObjectForKey:@"data"];
            [weakSelf.dataSource removeAllObjects];
            for (NSDictionary *modelDic in data) {
                ClassifyFirstModel *model = [ClassifyFirstModel mj_objectWithKeyValues:modelDic];
                [weakSelf.dataSource addObject:model];
            }
            NSLog(@"添加成功");
        } else {
            NSLog(@"单例分类请求失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
    } isNeedCache:NO];
}

- (ClassifyFirstModel *)getClassifyFirstModelWithGcid:(NSString *)gcid {
    if (!TextIsNotNil(gcid)) {
        ClassifyFirstModel *allTwoModel = [[ClassifyFirstModel alloc] init];
        allTwoModel.twoclass = [[NSMutableArray alloc] init];
        for (ClassifyFirstModel *firstModel in self.dataSource) {
            [allTwoModel.twoclass addObjectsFromArray:firstModel.twoclass];
        }
        return allTwoModel;
        //gcid = @"8";
    }
    for (ClassifyFirstModel *firstModel in self.dataSource) {
        if ([firstModel.gcid isEqualToString:gcid]) {
            return firstModel;
        }
    }
    NSLog(@"未找到gcid=%@的一级分类", gcid);
    return nil;
}

- (NSMutableArray <FiltrateFirstModel *> *)configureFiltrateModel:(ClassifyFirstModel *)firstModel {
    NSMutableArray *listArr = [NSMutableArray array];
    for (ClassifySecondModel *secondModel in firstModel.twoclass) {
        FiltrateFirstModel *filtrateFirstModel = [[FiltrateFirstModel alloc] init];
        filtrateFirstModel.title = secondModel.gtcname;
        filtrateFirstModel.gtcid = secondModel.gtcid;
        filtrateFirstModel.isShow = NO;
        NSMutableArray *secondArr = [NSMutableArray array];
        for (ClassifyThirdModel *thirdModel in secondModel.threeclass) {
            FiltrateSecondModel *filtrateSecondModel = [[FiltrateSecondModel alloc] init];
            filtrateSecondModel.title = thirdModel.gtrcname;
            filtrateSecondModel.gtrcid = thirdModel.gtrcid;
            filtrateSecondModel.isSelect = NO;
            [secondArr addObject:filtrateSecondModel];
        }
        filtrateFirstModel.secondList = secondArr;
        [listArr addObject:filtrateFirstModel];
    }
    return listArr;
}


@end
